#!/usr/bin/env pypy

import unittest, os

from datetime import datetime

import playlist_module

from catfile import CatFile
from settings import settings

class TestCatFile(unittest.TestCase):

	@classmethod
	def setUpClass(cls):
		cls.files = [os.path.join(u"data", u"test.{ext}".format(ext = ext)) for ext in ("ogg", "m4a", "mp3", "mpc", "flac")]

		with CatFile("test", path = ".", test = True) as catfile:
			for filename in cls.files:
				catfile += filename


	@classmethod
	def tearDownClass(cls):
		filename = settings.files.cat % "test"

		bakfile = "{filename}.{bak}".format(filename = filename, bak = settings.ext.bak)

		os.remove(filename)

		if os.path.exists(bakfile):
			os.remove(bakfile)


	def test_cat(self):
		with CatFile("test", path = ".", test = True) as catfile:
			self.assertEqual(catfile.cat, "test")


	def test_contains(self):
		with CatFile("test", path = ".", test = True) as catfile:
			self.assertIn(TestCatFile.files[0], catfile)


	def test_equals(self):
		with CatFile("test", path = ".", test = True) as catfile:
			self.assertEquals(catfile, set(TestCatFile.files))

	def test_not_equals(self):
		with CatFile("test", path = ".", test = True) as catfile:
			self.assertNotEquals(catfile, set(TestCatFile.files[1:]))

	def test_subset(self):
		with CatFile("test", path = ".", test = True) as catfile:
			self.assertLessEqual(catfile, set(TestCatFile.files))
			self.assertLessEqual(catfile, set(TestCatFile.files + ["BLAH"]))

	def test_superset(self):
		with CatFile("test", path = ".", test = True) as catfile:
			self.assertGreaterEqual(catfile, set(TestCatFile.files))
			self.assertGreaterEqual(catfile, set(TestCatFile.files[1:]))


	def test_copy(self):
		with CatFile("test", path = ".", test = True) as catfile:
			self.assertEqual(catfile, catfile.copy())

	def test_union(self):
		with CatFile("test", path = ".", test = True) as catfile:
			self.assertEqual(catfile | TestCatFile.files, catfile)

			cat2 = catfile.copy()

			cat2 -= TestCatFile.files[0]

			self.assertEqual(catfile | cat2, catfile)

	def test_add_track(self):
		with CatFile("test", path = ".", test = True) as catfile:
			cat2 = catfile - TestCatFile.files[0]

			cat2 += TestCatFile.files[0]

			self.assertEqual(cat2, catfile)

	def test_delete_track(self):
		with CatFile("test", path = ".", test = True) as catfile:
			cat2 = catfile - TestCatFile.files[0]

			self.assertEqual(cat2, set(TestCatFile.files[1:]))

	def test_intersection(self):
		with CatFile("test", path = ".", test = True) as catfile:
			self.assertEqual(catfile & TestCatFile.files[1:], set(TestCatFile.files[1:]))

			cat2 = catfile.copy()

			cat2 -= TestCatFile.files[0]

			self.assertEqual(catfile & cat2, cat2)

	def test_difference(self):
		with CatFile("test", path = ".", test = True) as catfile:
			self.assertEqual(catfile - TestCatFile.files[1:], set([TestCatFile.files[0]]))

			cat2 = catfile.copy()

			cat2 -= TestCatFile.files[0]

			cat3 = catfile - TestCatFile.files[1:]

			self.assertEqual(catfile - cat2, cat3)

	def test_symmetric_difference(self):
		with CatFile("test", path = ".", test = True) as catfile:
			cat2 = catfile - TestCatFile.files[0]

			cat3 = catfile - TestCatFile.files[-1]

			cat4 = cat2 ^ cat3

			self.assertEqual(cat4, set([TestCatFile.files[0], TestCatFile.files[-1]]))


	def test_disjoint(self):
		with CatFile("test", path = ".", test = True) as catfile:
			self.assertFalse(catfile.isdisjoint(TestCatFile.files))

			cat2 = catfile - TestCatFile.files[2:]

			cat3 = catfile - TestCatFile.files[0:2]

			self.assertTrue(cat2.isdisjoint(cat3))


if __name__ == '__main__':
	unittest.main()

